package com.hehe.controller.Photo;

import com.hehe.Common.IPUtil;
import com.hehe.POJO.Photo;
import com.hehe.POJO.Visitor_ip;
import com.hehe.Repository.CollectRepository;
import com.hehe.Repository.PhotoRepository;
import com.hehe.Repository.UserRepository;
import com.hehe.Repository.Visitor_IP_Repository;
import com.hehe.Service.CollectService;
import com.hehe.Service.UserService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

import java.util.Optional;

@Slf4j
@Controller
public class PhotoCon {


    @Value("${config_file_name}")
    private String config_file_name;


    @Resource
    private PasswordEncoder passwordEncoder;

    @Resource
    private UserRepository userRepository;

    @Resource
    private UserService userService;

    @Resource
    private Visitor_IP_Repository visitorIpRepository;

    @Resource
    private PhotoRepository photoRepository;

    @Resource
    private CollectRepository collectRepository;


    @GetMapping("/photo/{id}")
    public String photo_detail(HttpServletRequest request, Model model, @PathVariable Long id) {
        String usernameByCookie = userService.get_username_by_cookie(request);
        if (usernameByCookie == null) {
            usernameByCookie = "visitor";
        }

        // 获取 IP地址
        String requestIp = IPUtil.getRequestIp(request);
        log.info("访问者IP地址为: {}", requestIp);

        Visitor_ip visitor_ip = new Visitor_ip();
        visitor_ip.setUsername(usernameByCookie);
        visitor_ip.setPhotoId(id.intValue());
        visitor_ip.setIp(requestIp);
        visitorIpRepository.save(visitor_ip);

        model.addAttribute("usernameByCookie", usernameByCookie);
        Optional<Photo> photoOP = photoRepository.findById(id);
        if (photoOP.isPresent()) {
            Photo photo = photoOP.get();

            String[] split = photo.getImgPath().split(",");
            model.addAttribute("img_path", split);
            model.addAttribute("title", photo.getTitle());
            model.addAttribute("id", photo.getId());
            model.addAttribute("description", photo.getDescription());
            model.addAttribute("views", photo.getViews());
            model.addAttribute("price", photo.getPrice());
            model.addAttribute("collect", photo.getCollect());
        }
        /*
        * <!--<h1 th:text="${photo.title}"></h1>-->
<!--<h3 th:text="${photo.description}"></h3>-->
<!--<p><span th:text="${photo.views}"></span> 次浏览 </p>-->
<!--<p> 价格 <span th:text="${photo.price}"></span> 元 </p>-->
<!--<p> 发表于 <span th:text="${photo.uploadDate}"></span></p>-->
        *
        * */

        return "photo/photo.html";
    }


////    @ModelAttribute("loggedInUser")
////    public Tb_user getLoggedInUser(@CookieValue(value = "username", required = false) String username) {
////        if (username != null) {
////            // Simulate fetching user data from a service or database
////            Tb_user tb_user = new Tb_user();
////            tb_user.setUsername(username);
////            return tb_user;
////        }
////        return null;
////    }
//
//
//    @PostMapping("/login")
//    public String login(@RequestParam String username, @RequestParam String password, HttpServletResponse response, Model model) {
//        User tb_user = userRepository.findByUsername(username);
//        if (tb_user != null && passwordEncoder.matches(password, tb_user.getPassword())) { // 使用 BCrypt 验证密码
//            Cookie cookie = new Cookie("username", tb_user.getUsername());
//            cookie.setMaxAge(15 * 24 * 60 * 60); // 15 days
//            response.addCookie(cookie);
//            return "redirect:/login-success";
//        }
//        model.addAttribute("error", "用户名 或 密码 错误");
//        return "login/login.html";
//    }
//
//
//    @GetMapping("/login-success")
//    public String homePage(HttpServletRequest request) {
////        Cookie[] cookies = request.getCookies();
////        if (cookies != null) {
////            for (Cookie cookie : cookies) {
////                if ("username".equals(cookie.getName())) {
////                    System.out.println("用户已登录，继续处理请求");
////                    return "login-success.html";
////                }
////            }
////        }
////        return "login.html";
//        return "login/login-success.html";
//    }
//
//
//    @GetMapping("/logout")
//    public String logout(HttpServletRequest request, HttpServletResponse response) {
//        Cookie[] cookies = request.getCookies();
//        if (cookies != null) {
//            for (Cookie cookie : cookies) {
//                if ("username".equals(cookie.getName())) {
//                    cookie.setValue(null);
//                    cookie.setMaxAge(0);
//                    response.addCookie(cookie);
//                }
//            }
//        }
//        return "redirect:/";
//    }


}



